package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/packing")
@Slf4j
public class PackingController extends BaseController {

    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;




    // 装箱单页面
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "5") Integer pageSize){
        log.info("执行装箱单分页的方法");

        // 构造查询对象
        PackingExample example = new PackingExample();

        // 根据create_time 创建时间，倒序排序
        example.setOrderByClause("create_time desc");

        // 条件集合
        PackingExample.Criteria criteria = example.createCriteria();

        // 添加查询条件：compnay_id
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo<Packing> pageInfo = packingService.findByPage(example, pageNum, pageSize);

        // 存储查询结果
        request.setAttribute("pageInfo",pageInfo);

        return "cargo/packing/packing-list";
    }


    //跳转添加页面
    @RequestMapping("/toPacking")
    public String toPacking(String id){
        request.setAttribute("id",id);

        Packing packing = new Packing();
        Export export = exportService.findById(id);
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(export.getId());
        List<ExportProduct> exportProductList = exportProductService.findAll(exportProductExample);
        Double netWeight = 0d;
        Double grossWeight = 0d;
        Double totalVolume = 0d;
        for (ExportProduct exportProduct : exportProductList) {

            // 统计计算每个货物的毛重,净重,体积
            netWeight += exportProduct.getNetWeight();
            grossWeight += exportProduct.getGrossWeight();
            Double sizeHeight = exportProduct.getSizeHeight();
            Double sizeLength = exportProduct.getSizeLength();
            Double sizeWidth = exportProduct.getSizeWidth();
            totalVolume += sizeHeight*sizeLength*sizeWidth;
        }

        // 设置每个装箱单的总毛重,总净重,总体积
        packing.setGrossWeights(grossWeight);
        packing.setNetWeights(netWeight);
        packing.setTotalVolume(totalVolume);
        packing.setPackingMoney(grossWeight*520);

        // 设置报运合同号
        packing.setExportNos(export.getContractIds());

        // 设置装箱单主键
        packing.setPackingListId(UUID.randomUUID().toString());
        packing.setExportIds(export.getId());

        // 设置装箱单状态
        packing.setState(0);
        request.setAttribute("packing",packing);
        return "cargo/packing/packing-toPacking";

    }

    //添加
    @RequestMapping("/edit")
    public String edit(Packing packing){
        packing.setCompanyId(getLoginCompanyId());
        packing.setCompanyName(getLoginCompanyName());
        packingService.save(packing);

        return "redirect:/cargo/packing/list.do";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(String id){
        Export export = exportService.findById(id);
        packingService.delete(id);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping("/state")
    @ResponseBody
    public Map<String,Integer> state(String id) {
        Map<String,Integer> result = new HashMap<>();
        Packing packing = packingService.findById(id);
        if (packing.getState() == 0){
            // 该装箱单状态为草稿,可以删除
            result.put("message",1);
        }else {
            // 该装箱单为已委托,不可删除
            result.put("message",0);
        }
        return result;
    }



}